package best.driver.android.Utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import best.driver.android.R;
import best.driver.android.BestApp;

import java.io.ByteArrayOutputStream;
import java.io.File;

public class ImageLoaderUtil {

    private static ImageLoader imageLoader;
    private static final String GET_COMMON_IMAGE_LOADER_LOCK = "GET_COMMON_IMAGE_LOADER_LOCK";

    private static final String IMAGE_CACHE_FOLDER_NAME = "IMAGE_CACHE";
    private static final long CACHE_MAX_AGE = 1000 * 60 * 60 * 24;

    public static ImageLoader getImageLoader() {

        synchronized (GET_COMMON_IMAGE_LOADER_LOCK) {
            if (null == imageLoader) {
                File file = new File(Environment.getExternalStorageDirectory()
                        .getPath(), IMAGE_CACHE_FOLDER_NAME);
                if (!file.exists()) {
                    file.mkdir();
                }
                ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(BestApp.getInstance())
                        .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                        .discCacheFileNameGenerator(new Md5FileNameGenerator())
                        .discCache(new LimitedAgeDiscCache(file, CACHE_MAX_AGE))
                        .tasksProcessingOrder(QueueProcessingType.LIFO).build();
                imageLoader = ImageLoader.getInstance();
                imageLoader.init(config);
            }
            return imageLoader;
        }
    }

    public static DisplayImageOptions sOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.icon_photo)
            .showImageForEmptyUri(R.drawable.icon_photo)
            .showImageOnFail(R.drawable.icon_photo)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();


    public static void showBigWithCornerBitmap(final String uri,final ImageView view) {
        showImageBitmap(uri, view, null);
//        ImageLoaderUtil.getImageLoader().loadImage(uri, sOptions,
//                new ImageLoaderUtilListener() {
//                    @Override
//                    public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
//                        if (bitmap != null) {
//                            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                            view.setImageBitmap(Util.convertHeadPicture(bitmap));
//                            view.setTag(bitmap);
//                        }
//                    }
//                    @Override
//                    public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
//                    }
//                });
    }

    public static void showImageBitmap(final String uri,final ImageView view, DisplayImageOptions options) {
        if(TextUtils.isEmpty(uri)) {
            view.setImageResource(R.drawable.icon_photo);
            return;
        }
        if(options == null) {
            options = sOptions;
        }
        ImageLoaderUtil.getImageLoader().loadImage(uri, options,
                new ImageLoaderUtilListener() {
                    @Override
                    public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
                        if (bitmap != null) {
                            view.setImageBitmap(Util.convertHeadPicture(bitmap));
                        }
                    }

                    @Override
                    public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
                        //Toast.makeText(EDJApp.getInstance().getApplicationContext(), "kaofuckcao", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public static void showImageTriBitmap(final String uri,final ImageView view, DisplayImageOptions options) {
        if(TextUtils.isEmpty(uri)) {
            view.setImageResource(R.drawable.icon_photo);
            return;
        }
        if(options == null) {
            options = sOptions;
        }
        ImageLoaderUtil.getImageLoader().loadImage(uri, options,
                new ImageLoaderUtilListener() {
                    @Override
                    public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
                        if (bitmap != null) {
                            view.setImageBitmap(bitmap);
                        }
                    }

                    @Override
                    public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
                        //Toast.makeText(EDJApp.getInstance().getApplicationContext(), "kaofuckcao", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public static void showImageTriBitmap(final String uri,final ImageView view, DisplayImageOptions options, final ImageLoaderListener listener) {
        if(TextUtils.isEmpty(uri)) {
            view.setImageResource(R.drawable.icon_photo);
            return;
        }
        if(options == null) {
            options = sOptions;
        }
        ImageLoaderUtil.getImageLoader().loadImage(uri, options,
                new ImageLoaderUtilListener() {
                    @Override
                    public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
                        if (bitmap != null) {
                            view.setImageBitmap(bitmap);
                        }
                        if(listener != null) {
                            listener.onSuccess();
                        }
                    }

                    @Override
                    public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
                        //Toast.makeText(EDJApp.getInstance().getApplicationContext(), "kaofuckcao", Toast.LENGTH_SHORT).show();
                    }
                });
    }



    public static String bitmap2StrByBase64(Bitmap bit){
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 100, bos);//参数100表示不压缩
        byte[] bytes=bos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }



}
